<?php
/**
 # com_jvframework - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

class JVTheme{
    
    protected $activeTheme  = null;
    protected $defaultTheme = null;
    protected $baseTheme    = null;
        
    public function __construct() {
        $this->defaultTheme = $this->_getDefautTheme();
        $this->baseTheme = $this->_getBaseTheme();          
    }
    
    public static function getInstance(){
        static $instance;

		if (empty($instance)) { 
            $instance = new JVTheme();
		}
        
        return $instance;
    }
    
    public function getActiveTheme() {
        if(!$this->activeTheme){
            $this->activeTheme = $this->_getActiveTheme();    
        }
        
        return $this->activeTheme;
    }   
    
    public function getDefaultTheme() {
        return $this->defaultTheme;
    }
    
    public function getBaseTheme() {
        return $this->baseTheme;
    }
    
    private function _getBaseTheme(){
        $basetheme = new stdClass;
        $basetheme->path   = JPATH_SITE.DS.'components'.DS.'com_jvframework'.DS.'basethemes'; 
        $basetheme->layout = $basetheme->path . DS . 'layouts';
        $basetheme->url    = JURI::root(true).'/components/com_jvframework/basethemes';
        
        # Register Event plgJVFramework*::onGetBaseTheme()
        JVFactory::registerEvent('onGetBaseTheme', array(&$basetheme));
        
        return $basetheme; 
    }
    
    private function _getActiveTheme() {
        $activeTheme = $this->getDefaultTheme();   
        
        // Admin return current style
        if(JFactory::getApplication()->isAdmin()) return $activeTheme;   
           
        $activeTheme = is_object($activeTheme) ? $activeTheme : $this->findTheme($this->getTemplatePath() . DS . 'themes');
        
        if($activeTheme->params == ''){
            $activeTheme->params = $this->getDefautParameter($activeTheme->path);
        }
        
        $activeTheme->path   = $this->getTemplatePath() . DS . 'themes' . DS . $activeTheme->theme;
        $activeTheme->layout = $theme->path . DS . 'layouts';
        $activeTheme->url    = $this->getTemplateUrl().'/themes/'.$activeTheme->theme;
        
        # Register Event plgJVFramework*::onGetActiveTheme()
        JVFactory::registerEvent('onGetActiveTheme', array(&$activeTheme));        
        
        return $activeTheme;
    }
    
    protected function findTheme($path){
        jimport('joomla.filesystem.folder');
                        
        $folders = JFolder::folders($path);
        $theme   = new stdClass;
        
        if(is_array($folders)){
            foreach($folders as $folder){
                
                if(is_file($path . DS . $folder . DS . 'themeInfo.xml')){
                    $theme->id    = 9999;
                    $theme->name  = $folder.' (Auto find)';
                    $theme->theme = $folder;
                    
                    break;
                }
            } 
        }
        
        return $theme;
    }
    
    public function getDefautParameter($path){
        $path = $path.DS.'params.json';
        $param= '';
        
        if(JFile::exists($path)){
            $param = JFile::read($path);
        }
        
        # Register Event plgJVFramework*::onGetDefautParameter()
        JVFactory::registerEvent('onGetDefautParameter', array(&$param));
        
        if($param == '') return;
        
        return $param;
    }
    
    /**
     * Get default theme 
     * 
     * @access public
     * @return void
     */
    private function _getDefautTheme() {
        $db    = &JFactory::getDBO();        
        $query = 'SELECT * FROM #__jv_themes';
        
        // Get theme for edit
        if(JFactory::getApplication()->isAdmin()){
            $query .= ' WHERE id='.JRequest::getVar('id');
            
        }else{
            $query .= ' WHERE home=1';    
            
        }        
        
        $db->setQuery($query);        
        $theme = $db->loadObject();
        
        if(is_object($theme)){
            $theme->path = $this->getTemplatePath() . DS . 'themes' . DS . $theme->theme;
            $theme->url  = $this->getTemplateUrl().'/themes/'.$theme->theme;
        }
        
        # Register Event plgJVFramework*::getDefautTheme()
        JVFactory::registerEvent('onGetDefautTheme', array(&$theme));
        
        return $theme;
    }
    
    public function getTemplatePath(){
        return JPATH_THEMES.DS.$this->getSiteTemplate();
    }
    
    public function getTemplateUrl(){
        return JURI::root(true).'/'.$this->getSiteTemplate();
    }
    
    public function getSiteTemplate(){
        
        # J! 1.5
        if (version_compare(JVERSION, '1.6', '<')) {
            $query = 'SELECT template FROM #__templates_menu WHERE menuid=0 AND client_id = 0';
            
        # J! 1.6
        } else {
            $id    = 0;
            $app   = JFactory::getApplication();
            if ($app->isSite()) {
                $menus = JSite::getMenu();
                $menu = $menus->getActive();
                
                $id = isset($menu->template_style_id) ? $menu->template_style_id : $id;                
            }
            
            $query = 'SELECT template FROM #__template_styles  WHERE client_id = 0';
                        
            if($id){
                $query .= ' AND id ='.$id;
                
            }else{
                $query .= ' AND home = 1';                
            } 
        }
        
        $db = JFactory::getDBO();        
        $db->setQuery($query);
        
        return $db->loadResult();
    }
}